$(function () {
    $("#jqGrid").Grid({
        url: '../sparestorepos/list?lowestNum=1',
        rownumWidth: 50,
        height: 450,
        colModel: [
            {label: '备件名称', name: 'spareName', index: 'spareName', width: 80},
            {label: '备件规格', name: 'spec', index: 'spec', width: 80},
            {label: '备件编码', name: 'spareCode', index: 'SPARE_CODE', width: 50},
            {label: 'ERP编码', name: 'erpCode', index: 'SPARE_CODE', width: 50},
            {label: '供应厂商', name: 'producerName', width: 60},
            {label: '仓库名称', name: 'storeName', width: 120},
            {label: '库位', name: 'postion', index: 'POSTION', width: 60},
            {label: '库存数', name: 'storeNum', index: 'STORE_NUM', width: 40},
            {label: '冻结数', name: 'locNum', index: 'LOC_NUM', width: 40},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: 'storeId', name: 'storeId', index: 'storeId', hidden: true},
        ]
    });
    vm.getStore();
});

var ztree;

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    callback: {
        onClick: function () {
            // 重置仓库类别和仓库ID
            vm.q.storeType = '';
            vm.q.storeId = '';
            var node = ztree.getSelectedNodes();
            //选择上级菜单
            if (node[0].id.indexOf('AAA') !== -1) {

            }else if (node[0].id.indexOf('BBB') !== -1) {
                vm.q.storeType = 2;
            }else if (node[0].id.indexOf('CCC') !== -1) {
                vm.q.storeType = 1;
            }else {
                vm.q.storeId = node[0].id;
            }
            vm.query();
        }
    }
};


let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        spareStorePos: {
            storeName: '',
            postion: '',
            spareName: '',
            storeNum: '',
            id: '',
            targetStoreName: '', // 目标仓库名称
            targetPostion: '', // 目标库位
            targetSpareName: '', // 目标库位备件名称
            targetStoreNum: '', // 目标存储
            targetId: '', // 目标库位ID
        },
        q: {
            partName: '', // 备件名称
            spec: '', // 备件规格
            partCode: '', // 备件编码
            erpCode: '', // ERP编码
            producor:'', // 供应厂家
            storeId:'', // 所在仓库ID
            storeType: '' //仓库类别：1：ERP库 2:线边库
        }
    },
    methods: {
        exchangeStorePos: function(){
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.getInfo(id);
            if(vm.spareStorePos.storeNum <= 0){
                alert("只有库存数大于0的备件才可以执行移库操作");
                return;
            }
            // 库存转移
            vm.showList = false;
            vm.title = "备件移库";
        },
        chooseSpareStorePos: function(){
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择备件',
                content: '../spare/sparestorepos_choose.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSpareStroe();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        let storeNum = parseInt(res.storeNum);

                        if(storeNum == 0 || (storeNum - parseInt(res.locNum) <= 0)){
                            alert("可用库存不足，请更换!");
                            return;
                        }
                        layer.close(index);
                        vm.materialRecord.spareCode = res.spareCode;
                        vm.spareName = res.spareName;
                        vm.locNum = res.locNum;
                        vm.storeNum = res.storeNum;
                        vm.materialRecord.postion = res.postion;
                        vm.materialRecord.store = res.storeId;
                        vm.materialRecord.postionId = res.id;
                        vm.materialRecord.spareName =res.spareName;
                        // 默认为1
                        vm.materialRecord.num = 1;
                    }
                }
            })
        },
        query: function () {
            vm.reload();
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                partName: '', // 备件名称
                spec: '', // 备件规格
                partCode: '', // 备件编码
                erpCode: '', // ERP编码
                producor:'', // 供应厂家
                storeId:'', // 所在仓库ID
                storeType: ''
            };
            vm.reload();
        },
        getStore: function () {
            //加载树
            Ajax.request({
                url: "../storehouse/allList?moduleType=1",
                async: true,
                successCallback: function (r) {
                    var list = r.list;
                    var tree = [{id: 'AAA', name: '仓库', parentId: '0'},
                        {id: 'BBB', name: '线边库', parentId: 'AAA'},
                        {id: 'CCC', name: 'ERP库', parentId: 'AAA'}];
                    for (let i = 0; i < list.length; i++) {
                        var stoType = list[i].storeType;
                        if (stoType == "1") {
                            list[i].parentId = 'CCC'; //ERP库
                        } else {
                            list[i].parentId = 'BBB'; //线边库
                        }
                        tree.push(list[i]);
                    }
                    ztree = $.fn.zTree.init($("#storeTree"), setting, tree);
                }
            });
        }
    }
});
var getSpareStroe = function () {
    return getSelectedRowData("#jqGrid");
}
